package org.lq.dlz.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lq.dlz.common.Page;
import org.lq.dlz.dao.CategoryDao;
import org.lq.dlz.entity.Category;
import org.lq.dlz.entity.Product;
import org.lq.dlz.service.CategoryService;
import org.lq.dlz.service.ProductService;
import org.lq.dlz.service.impl.CategoryServiceImpl;
import org.lq.dlz.service.impl.ProductServiceImpl;

/**
 * Servlet implementation class CustomerCategoryServlet
 */
@WebServlet("/CustomerCategoryServlet.do")
public class CustomerCategoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    CategoryService categoryService  = new  CategoryServiceImpl();
    ProductService productService = new ProductServiceImpl();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CustomerCategoryServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
	if (method==null) {
		response.sendRedirect("CustomerCategoryServlet.do?method=homeData");
		return;
	}
	
		
	switch (method) {
	case "homeData":
		homeData(request, response);
		break;
		
	case "bycid":
		bycid(request, response);
		break;
	default:
		break;
	}
	
	}
	/**
	 * 分类展示
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void bycid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String cid = request.getParameter("cid");
			String index = request.getParameter("page");
			if(index==null) {
				index = "1";
			}
			
			List<Category> categoryList= categoryService.HomeCategoryList();
			request.setAttribute("categoryList", categoryList);
			Page<Product> pageProduct = new Page<Product>(Integer.valueOf(index), 9, categoryService.getCategoruByCidCount(Integer.valueOf(cid)));
			
			categoryService.getProductByCid(Integer.valueOf(cid), pageProduct);
			
			request.setAttribute("page", pageProduct);
			request.setAttribute("cid", cid);
			request.getRequestDispatcher("list.jsp").forward(request, response);	
		
	}
	
	/**
	 * 展示全部商品
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void homeData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			List<Category> categorys= categoryService.HomeCategoryList();
			request.getSession().setAttribute("categorys", categorys);
			
//			Map<Integer,List<Product>> hotProducts = new HashMap<>(); 
//			List<Product> hotList =productService.getHotProduct();
//			List<Product> list = null;
//			for (int i = 0; i < hotList.size(); i++) {
//				if(i%4 == 0) {
//					list = new ArrayList<Product>();
//					hotProducts.put(i,list);
//				}
//				list.add(hotList.get(i));		
//		}
//			request.getSession().setAttribute("hotProducts", hotProducts);
			response.sendRedirect("index.jsp");
	
		
		
	}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
